
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

import 'loding.dart';

class CommonUtil {
  // 收回键盘
  static void closeKeyBoard() {
    // 触摸收起键盘（方式一）
    FocusScopeNode currentFocus = FocusScope.of(Get.context!);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus?.unfocus();
    }
    // 触摸收起键盘（方式二）
    FocusScope.of(Get.context!).requestFocus(FocusNode());
  }

  static String hidePhoneMiddleNumber(String phone) {
    if (phone.length >= 7) {
      return '${phone.substring(0, 3)}****${phone.substring(phone.length - 4, phone.length)}';
    }
    return phone;
  }

  // 相机权限判断
  static Future<bool> isOpenCameraPermissions() async {
    if (await Permission.camera.request().isGranted) {
      return true;
    } else {
      xdShowToast('您未打开相机权限', durationTime: 3);
      return false;
    }
  }

  //相册权限
  static Future<bool> isOpenPhotosPermissions() async {
    if (GetPlatform.isAndroid) {
      if (await Permission.storage.request().isGranted) {
        return true;
      } else {
        xdShowToast('您未打开相册权限', durationTime: 3);
        return false;
      }
    } else {
      if (await Permission.photos.request().isGranted) {
        return true;
      } else {
        xdShowToast('您未打开相册权限', durationTime: 3);
        return false;
      }
    }

  }

  //麦克风权限
  static Future<bool> isOpenmicrophonePermissions() async {
    if (await Permission.microphone.request().isGranted) {
      return true;
    } else {
      xdShowToast('您未打开麦克风权限', durationTime: 3);
      return false;
    }
  }

}